package demo;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

//反射综合题
public class Day23001 {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchFieldException, ClassNotFoundException {
        Customer customer = new Customer();
        customer.setId(1);
        customer.setUsername("zhangsan");
        customer.setPassword("123456");

        Class clazz = customer.getClass();

        System.out.println(clazz);
//      打印所有成员变量
        System.out.println("-----------------");
        for (Field f :
                clazz.getDeclaredFields()) {
            System.out.println(f);
        }
//        打印所有构造方法
        System.out.println("------------------");
        for (Constructor c :
                clazz.getDeclaredConstructors()) {
            System.out.println(c);
        }
        System.out.println("------------------");
//        打印所有方法
        for (Method m :
                clazz.getDeclaredMethods()) {
            System.out.println(m);
        }
//      利用反射调用类中private属性和方法
        Constructor c = clazz.getDeclaredConstructor(Integer.class, String.class);
        c.setAccessible(true);
//        通过反射调用私有犯法构建一个对象o
        Object o = c.newInstance(2, "lisi");
        System.out.println(o);
//        通过反射调用私有成员变量给对象赋值
        Field f=clazz.getDeclaredField("password");
        f.setAccessible(true);
        f.set(o,"444444");
        System.out.println("=============");
        System.out.println(o);
        System.out.println("==============");
//        利用反射调用私有方法
        Method m=clazz.getDeclaredMethod("show");
        m.setAccessible(true);
        m.invoke(o);

    }
}

class Customer {
    private Integer id;
    private String username;
    private String password;

    public Customer() {
    }

    public Customer(Integer id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }

    //      私有构造方法
    private Customer(Integer id, String username) {
        this.id = id;
        this.username = username;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "Customer{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

    //    私有方法
    private void show() {
        System.out.println("我叫" + username);
    }

}